//
//  ButtonsFilter.m
//  EducationAdminClient
//
//  Created by kwsdzjx on 16/9/19.
//  Copyright © 2016年 周佳兴. All rights reserved.
//

#import "ButtonsFilter.h"
#define EQUAL @" == "
#define BUDENGYU @" != "
#define DAYU @" > "
#define XIAOYU @" < "
#define BUTTON_STRING   @"buttonString"     // 当前用户按钮权限字符串


@implementation ButtonsFilter

+(NSArray *)listDetailButtonArrFilter:(NSArray *)_buttons andDetailDic:(NSDictionary *)detailDic{
    
    return [self listDetailButtonArrFilter:_buttons andDetailDic:detailDic];
}
-(NSArray *)listDetailButtonArrFilter:(NSArray *)_buttons andDetailDic:(NSDictionary *)detailDic{
    
    _detailDic = detailDic;
    
    NSArray *detailAllKKKKKKeys = [_detailDic allKeys];
    NSMutableArray *showButtonsArr = [[NSMutableArray alloc]init];
    
    for (NSDictionary *willUseButton in _buttons) { // 权限 显示
        NSString *keyBtnStr = [NSString stringWithFormat:@"BTN_SHOW_%@", [willUseButton valueForKey:@"buttonId"]]; //1  和 除0都可以
        if([detailAllKKKKKKeys indexOfObject:keyBtnStr] != NSNotFound){
            
            // 存在即安装
            NSString *strBool = [NSString stringWithFormat:@"%@", [_detailDic valueForKey:keyBtnStr]];
            if ([strBool isEqualToString:@"1"]) {
                [showButtonsArr addObject:willUseButton];
            }
        }else{
            
            // 不存在 默认安装
            [showButtonsArr addObject:willUseButton];
        }
    }
    
    //*** 确认下课 --- 作业 按钮 二合一 字典
    showButtonsArr = [self buttonTwoToOne:showButtonsArr];
    
  /*  for (NSDictionary *willUseButton in _buttons) {
        NSString *useStr = [NSString stringWithFormat:@"%@", [willUseButton valueForKey:@"butWhereJs"]];//启用条件
        NSString *notStr = [NSString stringWithFormat:@"%@", [willUseButton valueForKey:@"butJyWhereJs"]];
        if (useStr.length<=0&&notStr.length<=0) { // 没有启用条件 没有禁用条件 直接使用该按钮
            [showButtonsArr addObject:willUseButton];
        }
        if (useStr.length>0) { //  启用条件
            
            NSString *componentString;
            
            if([useStr rangeOfString:EQUAL].location != NSNotFound){ // 等于的情况下
                componentString = EQUAL;
                
            }else if([useStr rangeOfString:BUDENGYU].location != NSNotFound){ // 不等于的情况下
                componentString = BUDENGYU;
            }else if([useStr rangeOfString:DAYU].location != NSNotFound){
                componentString = DAYU;

            }
            
            NSString *compareStr = [self compareString:useStr andComponentsString:componentString]; // [NSString stringWithFormat:@"%@", [_detailDic valueForKey:strUse]]; 启用条件值
            NSString *compareStr1 = [self valueString:useStr andComponentsString:componentString]; // 当前值

            if ([componentString isEqualToString:EQUAL]&&[compareStr isEqualToString:compareStr1]) { // 等于启用条件 ， 且是等于 启用
                [showButtonsArr addObject:willUseButton];
            }else if([componentString isEqualToString:BUDENGYU]&&![compareStr isEqualToString:compareStr1]){ // 不等于启用条件 ， 且不等于 启用
                [showButtonsArr addObject:willUseButton];
                
            }
        }
        
        if (notStr.length>0) {

            NSString *componentString;

            if([notStr rangeOfString:EQUAL].location != NSNotFound){
                componentString = EQUAL;
            }else if([notStr rangeOfString:BUDENGYU].location != NSNotFound){
                componentString = BUDENGYU;
            }else if([notStr rangeOfString:DAYU].location != NSNotFound){
                componentString = DAYU;
                
            }
            NSString *compareStr = [self compareString:notStr andComponentsString:componentString]; // [NSString stringWithFormat:@"%@", [_detailDic valueForKey:strUse]]; 禁用条件值
            NSString *compareStr1 = [self valueString:notStr andComponentsString:componentString]; // 当前值
            if ([componentString isEqualToString:EQUAL]&&![compareStr isEqualToString:compareStr1]) {
                [showButtonsArr addObject:willUseButton];
            }else if([componentString isEqualToString:BUDENGYU]&&[compareStr isEqualToString:compareStr1]){
                [showButtonsArr addObject:willUseButton];
            }
        }
        
    }*/
    return showButtonsArr;
}

#pragma mark - 按钮二合一
- (NSArray *)buttonTwoToOne:(NSArray *)showButtonsArr{
    
    
    NSMutableDictionary *parama = [[NSMutableDictionary alloc]init]; // 按钮二合一 即将 存在 这个字典中
    NSMutableArray      *saveButtons = [[NSMutableArray alloc]init]; // 暂时存储应该显示的按钮
    NSDictionary *dicCom;
    NSDictionary *dicHomeWork;
    
    if (showButtonsArr.count >= 2) {
        for (NSDictionary *dic in showButtonsArr) {
            NSString *nameStr1 = [NSString stringWithFormat:@"%@", [dic valueForKey:@"buttonName"]];
            if ([nameStr1 containsString:QUEREN]){
                dicCom = dic;
            }else if ([nameStr1 containsString:ZUOYE]){
                dicHomeWork = dic;
            }else{
                [saveButtons addObject:dic];
            }
        }
        
        
//        NSString *nameStr1 = [NSString stringWithFormat:@"%@", [showButtonsArr[0] valueForKey:@"buttonName"]];
//        NSString *nameStr2 = [NSString stringWithFormat:@"%@", [showButtonsArr[1] valueForKey:@"buttonName"]];
//        if (([nameStr1 containsString:@"确认下课"]||[nameStr1 containsString:@"作业"])&&([nameStr2 containsString:@"确认下课"]||[nameStr2 containsString:@"作业"])) {
//            [parama setObject:showButtonsArr[0] forKey:@""];
//            [parama setObject:showButtonsArr[0] forKey:@""];
//
//            
//        }
    }else{
        if (showButtonsArr.count == 1) {
            NSDictionary *dic = [showButtonsArr firstObject];
            [saveButtons addObject:dic];
        }
    }
    
    if (dicCom&&dicHomeWork) {
        [parama setObject:dicCom forKey:COMFIRM];
        [parama setObject:dicHomeWork forKey:HOMEWORK];
        [parama setObject:BUTTONNAME forKey:@"buttonName"];
        [parama setObject:@"YES" forKey:COMPOSESIGNKEYSTRING];
        [saveButtons addObject:parama];
    }
    NSLog(@"%zd",parama.count);
    if (parama.count>0) {
        
    }
    

    return saveButtons;

}
#define button ec是否存在

/**
 *  获取代码权限
 */
+(NSMutableArray *)textButton:(NSString *)pageId andBtnArr:(NSMutableArray *)buttons{
    // 先获取权限 是否有按钮权限 由于历史问题先做的这个功能
    
    NSMutableArray *buttons1 = [[NSMutableArray alloc]initWithArray:buttons];
    [buttons1 enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *str = [NSString stringWithFormat:@"%@_%@", pageId, [dic valueForKey:@"buttonId"]];
        BOOL can = [[self alloc] textButton: str]; // textButton(str);
        if (!can) {
            [buttons removeObject:dic];
        }
        
    }];
    
    return buttons1;
}

#pragma mark - 获取权限
-(BOOL)textButton:(NSString *)str{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *buttonString = [NSString stringWithFormat:@"%@", [userDefault valueForKey:BUTTON_STRING]];
    if([buttonString rangeOfString:str].location !=NSNotFound){
        return YES;
    }else{
        return NO;
    }
}

+(NSArray *)listDetailButtonArrFilterNew:(NSArray *)_buttons andDetailDic:(NSDictionary *)detailDic{
    
    return [self listDetailButtonArrFilterNew:_buttons andDetailDic:detailDic];
}

-(NSArray *)listDetailButtonArrFilterNew:(NSArray *)_buttons andDetailDic:(NSDictionary *)detailDic{
    NSMutableArray *showButtonsArr = [[NSMutableArray alloc]init];
    return nil;
}
/**
 *前提条件条件获取
 */
-(NSString *)compareString:(NSString *)notStr andComponentsString:(NSString *)component{
    NSArray *arrUsa = [notStr componentsSeparatedByString:component];
    NSString *str = [NSString stringWithFormat:@"%@", arrUsa[0]];
    NSArray *usageData = [str componentsSeparatedByString:@"."];
    NSString *strUse = usageData[1];

    NSString *compareStr = [NSString stringWithFormat:@"%@", [_detailDic valueForKey:strUse]];

    return compareStr;
}

/**
 *前提条件对应的值获取
 */
-(NSString *)valueString:(NSString *)notStr andComponentsString:(NSString *)component{
    NSArray *arrUsa = [notStr componentsSeparatedByString:component];
    NSString *str = [NSString stringWithFormat:@"%@", arrUsa[1]];
    
    NSString *compareStr;
    
    if([str rangeOfString:@"rowData."].location != NSNotFound){
        NSArray *usageData = [str componentsSeparatedByString:@"."];
        NSString *strUse = usageData[1];
        
        compareStr = [NSString stringWithFormat:@"%@", [_detailDic valueForKey:strUse]];

    }else{
        compareStr = [NSString stringWithFormat:@"%@", arrUsa[1]];
    }
    
    return compareStr;
}
@end
